using DealerPlatform.Common.RedisModule;
using DealerPlatform.Web.Models;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;

namespace DealerPlatform.Web.Controllers;

public class TestController(IRedisWorker redisWorker, RedisCore redisCore) : BaseController
{
    [HttpPost]
    public string Get()
    {
        List<UserInfo> userInfos = new();
        userInfos.Add(
            new()
            {
                Id = 1,
                UserName = "Ace",
                Age = 18
            });
        userInfos.Add(
            new()
            {
                Id = 2,
                UserName = "Taro",
                Age = 14
            });
        userInfos.Add(
            new()
            {
                Id = 3,
                UserName = "Leo",
                Age = 25
            });
        // redisWorker.SetHashMemory<UserInfo>("ultraman",userInfos, m =>
        // {
        //     var list = new List<string>();
        //     list.Add(m.Id.ToString());
        //     list.Add(m.UserName);
        //     return list;
        // });
        redisWorker.SetHashMemory<UserInfo>("ultraman", userInfos, m => new[] { m.Id.ToString(), m.UserName });
        redisWorker.SetHashMemory("user",new UserInfo{Id = 6,UserName = "Jack",Age = 39});
        return default;
    }

    [HttpGet("hash")]
    public List<UserInfo> GetHashEntries()
    {
        return redisWorker.GetHashMemory<UserInfo>("ultraman:*:*");
    }

    [HttpDelete]
    public void RemoveKey(string key)
    {
        redisCore.RemoveKey(key);
    }
}

public class UserInfo
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public int Age { get; set; }
}